<html>
    <head>
        <title>聊天室</title>
        <meta charset="utf-8"/>
        <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
        <link href="./css/style.css" rel="stylesheet"/>
        <script>
            var maxid = 0;
            function showmessage() {
                //创建ajax对象
                var xhr = new XMLHttpRequest();
                //监听
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        eval("var info=" + xhr.responseText);
                        var text = "";
                        for (var i = 0; i < info.length; i++)
                        {
                            text += "<div class='alert alert-"+info[i].color+"'>" +
                                    "<span class='name'>" + info[i].send + ":</span>" +
                                    "<span class='message'>" + info[i].content + "</span>" +
                                    "<span>(" + info[i].time+info[i].location + ")</span>" +
                                    "</div>";
                            maxid = info[i].id;
                        }
                        var old = document.getElementById("msg").innerHTML;
                        document.getElementById("msg").innerHTML = old + text;
                        if(info.length>0)
                        document.getElementById("msg").scrollTop = document.getElementById("msg").scrollHeight;
                    }
                };
                //初始化
                var talk = (GetQueryString("talk")==null)?"":"&talk="+GetQueryString("talk");
                xhr.open("get", "./action.php?maxid=" + maxid+talk);
                //发送
                xhr.send();
            }

function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
            $(document).ready(
                    function ()
                    {
                        showmessage();
                        self.setInterval("showmessage()", 2000);
                    }
            );
            function send() {
                var postData ="content="+document.getElementById('content').value;
                var xhr = new XMLHttpRequest();
                  var talk = (GetQueryString("talk")==null)?"":"?talk="+GetQueryString("talk");
                xhr.open("POST", "./add.php"+talk, true);
                //一定要写头信息不然服务器接收不到
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xhr.onreadystatechange = function () {
                    var XMLHttpReq = xhr;
                    if (XMLHttpReq.readyState == 4) {
                        if (XMLHttpReq.status == 200) {
                           document.getElementById('content').value="";
                        }
                    }
                };
                xhr.send(postData);
            }
        </script>
    </head>
    <body>

        <div class="panel panel-default" id="main">
            <!--聊天室名字-->    
            <div class="panel-heading">
                <h3 class="panel-title">
                    匿名者聊天室
                </h3>
                <a href="index.html?talk=selftalk">
                <button type="button" class="btn btn-info pull-right">  <span class="glyphicon glyphicon-hand-right"></span>私聊机器人</button>
                </a>
            </div>
            <!--聊天室名字-->
            <!--聊天室消息框-->
            <div class="panel-body ">
                <div class="well no-bottom ">
                    <!--                   消息框样式-->
                    <!--
                    <div class="alert alert-success">成功！很好地完成了提交。</div>
                    <div class="alert alert-info">信息！请注意这个信息。</div>
                    <div class="alert alert-warning">警告！请不要提交。</div>
                    <div class="alert alert-danger">错误！请进行一些更改。</div>
                    -->
                    <div id="msg" class="showmessage">
                    </div>
                    <!--聊天室消息框-->
                    <div class="well" >
                        <form role="form">
                            <div class="form-group">
                                <label for="name">发送消息</label>
                                <textarea class="form-control" id="content" name="content"
                                          style="resize: none;font-family: Microsoft YaHei;" rows="3" >
                                </textarea>
                            </div>
                            <div style="text-align: right">
                                <button type="button" class="btn btn-primary" onclick="send()">
                                    <span class="glyphicon glyphicon-envelope"></span>
                                    发送
                                </button>
                            </div>
                        </form>
                    </div>
                    <!--聊天室发送框-->
                </div>
            </div>
        </div>
    </body>
</html>